package com.common.project.util;

import com.common.project.common.ClientParams;
import com.common.project.enums.I18nEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.*;

/**
 * @ClassName I18nUtil
 * @Description
 * @Author jiangshicun@bicai365.com
 * @Date 2019/6/20 14:05
 */
public class I18nUtil {
    private static final Logger log = LoggerFactory.getLogger(I18nUtil.class);
    private static Map<String, List<Properties>> propertiesMaps = new HashMap();

    public I18nUtil() {
    }

    public static Map<String, List<Properties>> loadI18nProperties(String i18n) {
        if (propertiesMaps.get(i18n) != null && !((List)propertiesMaps.get(i18n)).isEmpty()) {
            return propertiesMaps;
        } else {
            try {
                List<Properties> list = (List)propertiesMaps.get(i18n);
                if (propertiesMaps.get(i18n) == null) {
                    list = new ArrayList();
                }

                String i18nCommonFile = MessageFormat.format("i18n/message_common{0}.properties", i18n);
                String i18nFile = MessageFormat.format("i18n/message{0}.properties", i18n);
                Resource resource1 = new ClassPathResource(i18nCommonFile);
                EncodedResource encodedResource1 = new EncodedResource(resource1, "UTF-8");
                Properties prop1 = PropertiesLoaderUtils.loadProperties(encodedResource1);
                if (prop1 != null) {
                    ((List)list).add(prop1);
                }

                Resource resource2 = new ClassPathResource(i18nFile);
                EncodedResource encodedResource2 = new EncodedResource(resource2, "UTF-8");

                try {
                    Properties prop2 = PropertiesLoaderUtils.loadProperties(encodedResource2);
                    if (prop2 != null) {
                        ((List)list).add(prop2);
                    }
                } catch (Exception var10) {
                    log.error("Local i81n Resource File Not Found..");
                }

                propertiesMaps.put(i18n, list);
            } catch (IOException var11) {
                log.error(var11.getMessage(), var11);
            }

            return propertiesMaps;
        }
    }

    public static String getString(String key) {
        String value = null;
        ClientParams clientParams = WebUtil.getClientParams();
        String language = clientParams != null ? clientParams.getLanguage() : I18nEnum.ZH_CN.getCode();
        String i18n = "_" + language;
        Map<String, List<Properties>> propertiesMaps = loadI18nProperties(i18n);
        if (propertiesMaps != null && !propertiesMaps.isEmpty()) {
            List<Properties> propertiesList = (List)propertiesMaps.get(i18n);
            if (propertiesList != null && propertiesList.size() != 0) {
                Iterator var7 = propertiesList.iterator();

                while(var7.hasNext()) {
                    Properties properties = (Properties)var7.next();
                    value = properties.getProperty(key);
                    if (StringUtils.isNotEmpty(value)) {
                        break;
                    }
                }
            }

            return value;
        } else {
            return null;
        }
    }
}
